package com.efesco.stat.tjbb.tag;

import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspException;
import java.lang.StringBuffer;
import java.io.IOException;
import java.util.*;
/*
 * author:linzihui
 * email:boboif@tom.com
 * verion:1.0
 * */
public class ElementTag extends TagSupport {
  private String name;
  private String stat;
  private String format;
  private String nullvalue="&nbsp;";
  private String editabled = "false";
  private String value;

  public int doStartTag() throws JspException{
    StringBuffer sb = new StringBuffer();
    HashMap hmValue = (HashMap)pageContext.getRequest().getAttribute("ReportData");
    String strTemp = "";

    if (editabled.toLowerCase().equals("false"))
      sb.append("<span editabled='false' id='" + name + "'");
    else
      sb.append("<input type='text' class='rptedit' editabled='true' id='" + name + "' name='"+name+"'");
    if (stat != null && stat.length() > 0) sb.append(" stat=\"" + stat + "\"");
    if (nullvalue != null) sb.append(" nullvalue=\"" + nullvalue + "\"");
    if (format != null && format.length() > 0) sb.append(" format=\"" + format + "\"");

   if (hmValue == null)
      strTemp = "";
    else{
      strTemp = (String) hmValue.get(name);
      if (strTemp == null) strTemp = "";
      if (strTemp.length()>0&&format != null&&format.length()>0){
        int t = 10;
        for (int i=1; i<Integer.parseInt(format);i++)
          t = t * 10;
        double dltmp = Math.round(Double.parseDouble(strTemp)*t);
        strTemp = String.valueOf(dltmp/t);
      }
    }
    if (editabled.toLowerCase().equals("false")){
      sb.append(">");
      sb.append(strTemp);
    }
    else{
      if(value!=null&&value.length()>0)
        sb.append(" value='"+value+"'>");
      else
        sb.append(" value='" + strTemp + "'>");
    }
//System.out.println("===="+sb.toString());
    display(sb.toString());
    return 0;
  }
  public int doEndTag() throws JspException{
    if (editabled.toLowerCase().equals("false"))
      display("</span>");
    return 6;
  }
  //print String
  private void display(String outText){
    try{
      pageContext.getOut().print(outText);
    }
    catch (IOException ie){
      System.out.println("doEndTag IOException:" + ie.getMessage());
    }
  }
  //property mehtod defined
  public void setName(String value){
    this.name = "rpt_" + value;
  }
  public void setStat(String value){
    this.stat = value;
  }
  public void setFormat(String value){
    this.format = value;
  }
  public void setNullvalue(String value){
    this.nullvalue = value;
  }
  public void setEditabled(String value){
    this.editabled = value;
  }
  public void setValue(String value) {
    this.value = value;
  }

}
